package demo.service;

import demo.Result;
import demo.entity.Department;
import demo.repository.CompanyRepository;
import demo.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门 Service
 *
 * @author 肖溥博文
 * @version 1.6
 */
@Service
public class DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    CompanyRepository companyRepository;

    /**
     * 添加部门
     *
     * @param departmentName 部门 id
     * @param companyId      公司 id
     * @return 提示信息
     */
    public Result add(String departmentName, Long companyId) {
        Department department = new Department();
        department.setDepartmentName(departmentName);
        department.setCompanyId(companyId);
        department.setUsable(true);
        department = departmentRepository.save(department);
        return Result.success("部门添加成功", department);
    }

    /**
     * 按部门 id 查找部门
     *
     * @param departmentId 部门 id
     * @return 部门对象
     */
    public Department findByDepartmentId(Long departmentId) {
        return departmentRepository.findByDepartmentId(departmentId);
    }

    /**
     * 按部门 id 查找目前仍存在的部门
     *
     * @param departmentId 部门 id
     * @return 部门对象
     */
    public Department findByDepartmentIdAndUsable(Long departmentId) {
        return departmentRepository.findByDepartmentIdAndUsable(departmentId, true);
    }

    /**
     * 按 公司 id 和部门名查找现存部门
     *
     * @param companyId      公司 id
     * @param departmentName 部门名
     * @return 部门结果
     */
    public Result findByCompanyIdAndDepartmentNameAndUsable(Long companyId, String departmentName) {
        Boolean isFound = departmentRepository.existsByCompanyIdAndDepartmentNameAndUsable(companyId, departmentName, true);
        if (!isFound) {
            return Result.error("没有找到该部门");
        }
        Department department = departmentRepository.findAllByCompanyIdAndDepartmentNameAndUsable(companyId, departmentName, true).get(0);
        return Result.success("已找到该部门", department);
    }

    /**
     * 在添加部门时按 公司 id 和 部门名 查找部门
     *
     * @param companyId      公司 id
     * @param departmentName 部门名
     * @return 部门结果
     */
    public Boolean findByCompanyIdAndDepartmentName(Long companyId, String departmentName) {
        Boolean isFound = departmentRepository.existsByCompanyIdAndDepartmentNameAndUsable(companyId, departmentName, true);
        return isFound;
    }

    /**
     * 通过公司 id 获取所有部门的信息
     *
     * @param companyId 公司 id
     * @return 结果对象，包含该公司所有部门对象
     */
    public Result listDepartments(Long companyId) {
        return Result.success("部门查询成功", departmentRepository.findDepartmentsByCompanyIdAndUsable(companyId, true));
    }

    /**
     * 列出所有部门
     *
     * @return 所有部门
     */
    public List<Department> listDepartments() {
        return departmentRepository.findAll();
    }


    /**
     * 更新部门
     *
     * @param department 部门对象
     * @return 结果对象
     */
    public Result update(Department department) {
        departmentRepository.save(department);
        return Result.success("更新成功");
    }
}
